package com.example.mymicaihu.fragment.home;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mvplibrary.ui.fragment.BaseMvpFragment;
import com.example.mymicaihu.R;
import com.example.mymicaihu.adapter.home.FocusNewsAdapter;
import com.example.mymicaihu.bean.FocusNewsBean;
import com.example.mymicaihu.callback.Mainconcations;
import com.example.mymicaihu.model.MainModel;
import com.example.mymicaihu.presenter.MainPresenter;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * 作者：朱勇闯
 * data : 2021/4/25  14:44
 * 作用： 首页要闻
 */
public class FocusNewsFragment<T> extends BaseMvpFragment<Mainconcations.MainView, MainModel, MainPresenter<T>> implements Mainconcations.MainView {
    private List<FocusNewsBean.DataBean> focusList;
    @BindView(R.id.rv_focusnews)
    RecyclerView rv_list;
    private FocusNewsAdapter focusNewsAdapter;

    @Override
    protected MainPresenter<T> createPresenter() {
        return new MainPresenter();
    }

    @Override
    protected void initEvent() {
        focusList = new ArrayList<>();
        rv_list.setLayoutManager(new LinearLayoutManager(context));
        focusNewsAdapter = new FocusNewsAdapter(focusList, context, R.layout.item_focusnews);
        rv_list.setAdapter(focusNewsAdapter);
        HashMap<String, Object> mapTab = new HashMap<>();
        mapTab.put("cv", "2.5.3");
//        mapTab.put("ms", "MSMyLjUuMyMyMDIwMTIwMSNtYXJrZXRfdml2byMxI3Zpdm92aXZvIFg3I2ZmZmZmZmZmLWU4ZTQtMjIzZC1mZmZmLWZmZmZlNjRiYzYzYg");
        mapTab.put("ms", "MSMyLjUuMyMyMDIwMTIwMSNtYXJrZXRfdml2byMxI3Zpdm92aXZvIFg3I2ZmZmZmZmZmLWU4ZTQtMjIzZC1mZmZmLWZmZmZlNjRiYzYzYg==");
        mapTab.put("referer", "http://api.micaiying.com");
        mPresenter.getData(mapTab, "http://api.micaiying.com//", "news/topNewsDate", 0);
    }

    @Override
    protected int createLayoutId() {
        return R.layout.fragment_home_focusnews;
    }

    @Override
    public void showList(Object t, int type) {
        if (type == 0) {
            Gson gson = new Gson();
            Type type1 = new TypeToken<List<FocusNewsBean.DataBean>>() {
            }.getType();
            List<FocusNewsBean.DataBean> focusNewsBean = gson.fromJson((JsonElement) t, type1);
            for (int i = 0; i < focusNewsBean.size(); i++) {
                focusList.add(focusNewsBean.get(i));
            }
            focusNewsAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showError(String error) {

    }

    @Override
    public void showProgressbar() {

    }

    @Override
    public void hideProgressbar() {

    }
}
